#include "VerticesConstructor.h"
#include "QD3D\QD3DUtils.h"

VerticesConstructor::VerticesConstructor (const ReconstructConfig& iConfig) :
		ProcessBase (iConfig)
{
	_pVerticesConstructDxCS = NULL;
}

VerticesConstructor::~VerticesConstructor ()
{
}

void VerticesConstructor::initialize ()
{
	QD3DUtils d3DUtils;

	QD3DShaderDesc shaderDesc;
	shaderDesc.setDefine ("FACE_THRESH", config.faceThreshStr);
	shaderDesc.setDefine ("REGION_SIZE", config.reconstructRegionStr);
	shaderDesc.setDefine ("FW", config.frameWidthStr);
	shaderDesc.setDefine ("FH", config.frameHeightStr);
	_pVerticesConstructDxCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			config.shaderRoot + "VerticesConstructCS.hlsl", "csMain", shaderDesc);
}

void VerticesConstructor::clean ()
{
	SAFE_RELEASE (_pVerticesConstructDxCS);
}

void VerticesConstructor::process (QVector< SensorResource > &sensorReses)
{
	for (int i = 0; i < sensorReses.size (); ++i)
		process (sensorReses[i]);
}

void VerticesConstructor::process (SensorResource &sensorRes)
{
	QD3DUtils d3DUtils;

	QVector< ID3D11Buffer * > dxConstBufPtrs;
	QVector< ID3D11ShaderResourceView * > dxSRVPtrs(2);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs(1);

	dxConstBufPtrs.push_back (sensorRes.p_sensorIntDxConstBuf);
	dxConstBufPtrs.push_back (sensorRes.p_sensorExtDxConstBuf);
	dxConstBufPtrs.push_back (sensorRes.p_verticesOffsetDxConstBuf);
// 	dxConstBufPtrs.push_back (sensorRes.p_sensorParaDxConstBuf);
// 	dxConstBufPtrs.push_back (sensorRes.p_verticesOffsetDxConstBuf);
	d3DUtils.setConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstBufPtrs);
	dxSRVPtrs[0] = sensorRes.p_rgbPack->getSRV ();
	dxSRVPtrs[1] = sensorRes.p_depthPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = sensorRes.rp_verticesPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_pVerticesConstructDxCS);
	d3DUtils.dispatch (config.frameHeight, 1, 1);
	d3DUtils.clearConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstBufPtrs.size ());
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}
